package com.easylinkin.linkappapi.meterbilling.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.bases.redis.util.RedisUtil;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.common.utils.message.SendMessageUtil;
import com.easylinkin.linkappapi.meterbilling.entity.ResidentInfo;
import com.easylinkin.linkappapi.meterbilling.service.ResidentService;
import com.easylinkin.linkappapi.meterbilling.util.RechargeConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.List;


/**
 * class info :住户控制类
 *
 * @author liuqihang
 * @date 2021/7/17 15:24
 */
@Api(tags={"住户信息控制器"})
@RestController
@RequestMapping("/resident")
public class ResidentController {

    @Resource
    private ResidentService residentService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    SendMessageUtil sendMessageUtil;

    @ApiOperation("新增")
    @PostMapping("save")
    public RestMessage save(@RequestBody ResidentInfo residentInfo) {
        Assert.notNull(residentInfo, "住户数据不能为空");
        String result = residentService.saveResidentInfo(residentInfo);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @ApiOperation("修改")
    @PostMapping("update")
    public RestMessage update(@RequestBody ResidentInfo residentInfo) {
        Assert.notNull(residentInfo, "住户数据不能为空");
        String result = residentService.updateResidentInfo(residentInfo);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    public RestMessage delete(@RequestBody ResidentInfo residentInfo) {
        Assert.notNull(residentInfo, "住户数据不能为空");
        Assert.notNull(residentInfo.getId(), "住户ID不能为空");
        String result = residentService.delete(residentInfo.getId());
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @ApiOperation("根据ID获取详情")
    @PostMapping("getInfo")
    public RestMessage getInfo(@RequestBody ResidentInfo residentInfo) {
        Assert.notNull(residentInfo, "房间数据不能为空");
        Assert.notNull(residentInfo.getId(), "住户ID不能为空");
        ResidentInfo result = residentService.getResidentInfoById(residentInfo.getId());
        return RestBuilders.successBuilder().data(result).build();
    }

    @ApiOperation("分页查询")
    @PostMapping("getResidentInfoPage")
    public RestMessage getResidentInfoPage(@RequestBody RequestModel<ResidentInfo> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "查询参数 不能为空");
        IPage<ResidentInfo> roomPage = residentService.getResidentInfoPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(roomPage).build();
    }


    @ApiOperation("获取全部住户list")
    @PostMapping("getResidentInfoList")
    public RestMessage getResidentInfoList(@RequestBody ResidentInfo residentInfo) {
        Assert.notNull(residentInfo, "requestModel 不能为空");
        List<ResidentInfo> roomPage = residentService.getResidentInfoList(residentInfo);
        return RestBuilders.successBuilder().data(roomPage).build();
    }

    @ApiOperation("修改密码")
    @PostMapping("updatePwd")
    public RestMessage updatePwd(@RequestBody ResidentInfo residentInfo) {
        Assert.notNull(residentInfo, "住户数据不能为空");
        Assert.notNull(residentInfo.getId(), "住户ID不能为空");
        Assert.notNull(residentInfo.getPassword(), "密码不能为空");
        residentService.updatePwd(residentInfo);
        return RestBuilders.successBuilder().message("success").build();
    }

    @ApiOperation("忘记密码--修改")
    @PostMapping("updatePwdByForget")
    public RestMessage updatePwdByForget(@RequestBody ResidentInfo residentInfo) {
        Assert.notNull(residentInfo, "住户数据不能为空");
        Assert.notNull(residentInfo.getPhone(), "手机号不能为空");
        Assert.notNull(residentInfo.getPassword(), "密码不能为空");
        residentService.updatePwd(residentInfo);
        return RestBuilders.successBuilder().message("success").build();
    }

    /**
     * 根据手机发送验证码
     * @param phone
     * @param funcFlag (0:登录， 1：修改密码)
     * @return
     */
    @ApiOperation("根据手机号获取验证码")
    @PostMapping("/verificationCode")
    public RestMessage verificationCode(@RequestParam("phone") String phone, @RequestParam("funcFlag") Integer funcFlag) {
        Assert.notNull(funcFlag, "功能标识符不能为空");
        Assert.notNull(phone, "手机号不能为空");
        ResidentInfo ri = residentService.getResidentInfoByPhone(phone);
        Assert.notNull(ri, "通过用户名查找不到用户");
        int randomInt = (int) ((Math.random() * 999999));
        String randomNo = String.format("%6d", randomInt).replace(" ", "0");
        if(funcFlag.equals(0)){
            redisUtil.set(RechargeConstant.METER_WECHAT_PUBLIC_ACCOUNT_LOGIN_VERIFICATION_CODE_PREFIX + phone, randomNo, 300);
        }else if(funcFlag.equals(1)){
            redisUtil.set(RechargeConstant.METER_WECHAT_PUBLIC_ACCOUNT_UPDATE_VERIFICATION_CODE_PREFIX_ + phone, randomNo, 300);
        }else {
            return RestBuilders.failureMessage();
        }
        sendMessageUtil.sendVerificationCode(phone, randomNo);
        return RestBuilders.successMessage();
    }

    /**
     * 验证用户填写的手机验证码是否正确
     * @param phone
     * @param code
     * @return
     */
    @ApiOperation("验证验证码填写是否正确")
    @PostMapping("/checkVerificationCode")
    public RestMessage checkVerificationCode(@RequestParam("phone") String phone, @RequestParam("code") String code, @RequestParam("funcFlag") Integer funcFlag) {
        Assert.notNull(phone, "手机号不能为空");
        Assert.notNull(code, "请填写验证码");
        Assert.notNull(funcFlag, "功能标识符不能为空");
        ResidentInfo ri = residentService.getResidentInfoByPhone(phone);
        Assert.notNull(ri, "通过用户名查找不到用户");

        Object o = null;
        if(funcFlag.equals(0)){
            o = redisUtil.get(RechargeConstant.METER_WECHAT_PUBLIC_ACCOUNT_LOGIN_VERIFICATION_CODE_PREFIX + phone);
        }else if(funcFlag.equals(1)){
            o = redisUtil.get(RechargeConstant.METER_WECHAT_PUBLIC_ACCOUNT_UPDATE_VERIFICATION_CODE_PREFIX_ + phone);
        }else {
            o = null;
        }

        if(StringUtils.isEmpty(o)){
            return RestBuilders.failureBuilder().message("验证码无效或过期，请获取验证码").build();
        }
        String verificationCode = (String) o;
        if(!code.equals(verificationCode)){
            return RestBuilders.failureBuilder().message("验证码填写错误").build();
        }
        return RestBuilders.successMessage();
    }

    @ApiOperation("根据手机号获取用户信息")
    @PostMapping("getResidentInfoByPhoneApp")
    public RestMessage getResidentInfoByPhoneApp(@RequestBody ResidentInfo residentInfo) {
        ResidentInfo residentInfoByPhoneApp = residentService.getResidentInfoByPhoneApp(residentInfo);
        return RestBuilders.successBuilder().data(residentInfoByPhoneApp).build();
    }
}
